﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALNews
    /// </summary>
    public partial class DALNews
    {
        public DALNews()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int newsId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbNews");
            strSql.Append(" where newsId=@newsId");
            SqlParameter[] parameters = {
					new SqlParameter("@newsId", SqlDbType.Int,4)
			};
            parameters[0].Value = newsId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbNews model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbNews(");
            strSql.Append("typeId,newsName,newsContent,newsOrder,visible,author,updateTime,isNew,email,keys,newsUrl,newsFile,newsFileUrl)");
            strSql.Append(" values (");
            strSql.Append("@typeId,@newsName,@newsContent,@newsOrder,@visible,@author,@updateTime,@isNew,@email,@keys,@newsUrl,@newsFile,@newsFileUrl)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@typeId", SqlDbType.Int,4),
					new SqlParameter("@newsName", SqlDbType.NVarChar,150),
					new SqlParameter("@newsContent", SqlDbType.NVarChar),
					new SqlParameter("@newsOrder", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@author", SqlDbType.NVarChar,50),
					new SqlParameter("@updateTime", SqlDbType.DateTime),
					new SqlParameter("@isNew", SqlDbType.Bit,1),
					new SqlParameter("@email", SqlDbType.NVarChar,200),
					new SqlParameter("@keys", SqlDbType.NVarChar,250),
					new SqlParameter("@newsUrl", SqlDbType.NVarChar,150),
					new SqlParameter("@newsFile", SqlDbType.NVarChar,150),
					new SqlParameter("@newsFileUrl", SqlDbType.NVarChar,150)};
            parameters[0].Value = model.typeId;
            parameters[1].Value = model.newsName;
            parameters[2].Value = model.newsContent;
            parameters[3].Value = model.newsOrder;
            parameters[4].Value = model.visible;
            parameters[5].Value = model.author;
            parameters[6].Value = model.updateTime;
            parameters[7].Value = model.isNew;
            parameters[8].Value = model.email;
            parameters[9].Value = model.keys;
            parameters[10].Value = model.newsUrl;
            parameters[11].Value = model.newsFile;
            parameters[12].Value = model.newsFileUrl;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbNews model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbNews set ");
            strSql.Append("typeId=@typeId,");
            strSql.Append("newsName=@newsName,");
            strSql.Append("newsContent=@newsContent,");
            strSql.Append("newsOrder=@newsOrder,");
            strSql.Append("visible=@visible,");
            strSql.Append("author=@author,");
            strSql.Append("updateTime=@updateTime,");
            strSql.Append("isNew=@isNew,");
            strSql.Append("email=@email,");
            strSql.Append("keys=@keys,");
            strSql.Append("newsUrl=@newsUrl,");
            strSql.Append("newsFile=@newsFile,");
            strSql.Append("newsFileUrl=@newsFileUrl,");
            strSql.Append("clicks=@clicks");
            strSql.Append(" where newsId=@newsId");
            SqlParameter[] parameters = {
					new SqlParameter("@typeId", SqlDbType.Int,4),
					new SqlParameter("@newsName", SqlDbType.NVarChar,150),
					new SqlParameter("@newsContent", SqlDbType.NVarChar),
					new SqlParameter("@newsOrder", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@author", SqlDbType.NVarChar,50),
					new SqlParameter("@updateTime", SqlDbType.DateTime),
					new SqlParameter("@isNew", SqlDbType.Bit,1),
					new SqlParameter("@email", SqlDbType.NVarChar,200),
					new SqlParameter("@keys", SqlDbType.NVarChar,250),
					new SqlParameter("@newsUrl", SqlDbType.NVarChar,150),
					new SqlParameter("@newsFile", SqlDbType.NVarChar,150),
					new SqlParameter("@newsFileUrl", SqlDbType.NVarChar,150),
                    	new SqlParameter("@clicks", SqlDbType.Int,4),
					new SqlParameter("@newsId", SqlDbType.Int,4)};
            parameters[0].Value = model.typeId;
            parameters[1].Value = model.newsName;
            parameters[2].Value = model.newsContent;
            parameters[3].Value = model.newsOrder;
            parameters[4].Value = model.visible;
            parameters[5].Value = model.author;
            parameters[6].Value = model.updateTime;
            parameters[7].Value = model.isNew;
            parameters[8].Value = model.email;
            parameters[9].Value = model.keys;
            parameters[10].Value = model.newsUrl;
            parameters[11].Value = model.newsFile;
            parameters[12].Value = model.newsFileUrl;
            parameters[13].Value = model.Clicks;
            parameters[14].Value = model.newsId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int newsId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbNews ");
            strSql.Append(" where newsId=@newsId");
            SqlParameter[] parameters = {
					new SqlParameter("@newsId", SqlDbType.Int,4)
			};
            parameters[0].Value = newsId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string newsIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbNews ");
            strSql.Append(" where newsId in (" + newsIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbNews GetModel(int newsId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 newsId,typeId,isnull(clicks,0) as clicks, newsName,isnull(newsContent,'') as newsContent ,newsOrder,visible,author,updateTime,isNew,email,keys,newsUrl,newsFile,newsFileUrl from tbNews ");
            strSql.Append(" where newsId=@newsId");
            SqlParameter[] parameters = {
					new SqlParameter("@newsId", SqlDbType.Int,4)
			};
            parameters[0].Value = newsId;

            Gift.Model.tbNews model = new Gift.Model.tbNews();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["newsId"] != null && ds.Tables[0].Rows[0]["newsId"].ToString() != "")
                {
                    model.newsId = int.Parse(ds.Tables[0].Rows[0]["newsId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["typeId"] != null && ds.Tables[0].Rows[0]["typeId"].ToString() != "")
                {
                    model.typeId = int.Parse(ds.Tables[0].Rows[0]["typeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["newsName"] != null && ds.Tables[0].Rows[0]["newsName"].ToString() != "")
                {
                    model.newsName = ds.Tables[0].Rows[0]["newsName"].ToString();
                }

                    model.newsContent = ds.Tables[0].Rows[0]["newsContent"].ToString();

                if (ds.Tables[0].Rows[0]["newsOrder"] != null && ds.Tables[0].Rows[0]["newsOrder"].ToString() != "")
                {
                    model.newsOrder = int.Parse(ds.Tables[0].Rows[0]["newsOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["visible"] != null && ds.Tables[0].Rows[0]["visible"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["visible"].ToString() == "1") || (ds.Tables[0].Rows[0]["visible"].ToString().ToLower() == "true"))
                    {
                        model.visible = true;
                    }
                    else
                    {
                        model.visible = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["author"] != null && ds.Tables[0].Rows[0]["author"].ToString() != "")
                {
                    model.author = ds.Tables[0].Rows[0]["author"].ToString();
                }
                if (ds.Tables[0].Rows[0]["updateTime"] != null && ds.Tables[0].Rows[0]["updateTime"].ToString() != "")
                {
                    model.updateTime = DateTime.Parse(ds.Tables[0].Rows[0]["updateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isNew"] != null && ds.Tables[0].Rows[0]["isNew"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isNew"].ToString() == "1") || (ds.Tables[0].Rows[0]["isNew"].ToString().ToLower() == "true"))
                    {
                        model.isNew = true;
                    }
                    else
                    {
                        model.isNew = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["email"] != null && ds.Tables[0].Rows[0]["email"].ToString() != "")
                {
                    model.email = ds.Tables[0].Rows[0]["email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["keys"] != null && ds.Tables[0].Rows[0]["keys"].ToString() != "")
                {
                    model.keys = ds.Tables[0].Rows[0]["keys"].ToString();
                }
                if (ds.Tables[0].Rows[0]["newsUrl"] != null && ds.Tables[0].Rows[0]["newsUrl"].ToString() != "")
                {
                    model.newsUrl = ds.Tables[0].Rows[0]["newsUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["newsFile"] != null && ds.Tables[0].Rows[0]["newsFile"].ToString() != "")
                {
                    model.newsFile = ds.Tables[0].Rows[0]["newsFile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["newsFileUrl"] != null && ds.Tables[0].Rows[0]["newsFileUrl"].ToString() != "")
                {
                    model.newsFileUrl = ds.Tables[0].Rows[0]["newsFileUrl"].ToString();
                }

                model.Clicks = int.Parse( ds.Tables[0].Rows[0]["clicks"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select newsId,typeId,newsName,newsContent,newsOrder,visible,author,updateTime,isNew,email,keys,newsUrl,newsFile,newsFileUrl ");
            strSql.Append(" FROM tbNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" newsId,typeId,newsName,newsContent,newsOrder,visible,author,updateTime,isNew,email,keys,newsUrl,newsFile,newsFileUrl ");
            strSql.Append(" FROM tbNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.newsId desc");
            }
            strSql.Append(")AS Row, T.*,(select typeName from tbNewsType where typeId = T.TypeId) as typeName from tbNews T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbNews";
            parameters[1].Value = "newsId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

